package com.wh1200.mybatis.xmlless.resolver

import com.wh1200.mybatis.xmlless.config.BaseResolverTest
import com.wh1200.mybatis.xmlless.enums.XmllessDbType
import com.wh1200.mybatis.xmlless.util.ColumnUtils
import com.wh1200.mybatis.xmlless.util.DbTypeHolder
import com.wh1200.mybatis.xmlless.util.XmllessInfoUtil
import com.wh1200.xmlless.test.dao.StudentDAO
import com.wh1200.xmlless.test.model.Student
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import kotlin.reflect.jvm.javaMethod

/**
 * Created by 吴昊 on 2023/12/10.
 */
class ColumnsResolverTest : BaseResolverTest(
    StudentDAO::class.java
) {

  @Test
  fun wrapColumn() {
    val dbType = DbTypeHolder.getDBType()
    XmllessInfoUtil.setDbType(XmllessDbType.MYSQL)
    assertEquals("`desc`", ColumnUtils.wrapColumn("desc"))
    assertEquals("password", ColumnUtils.wrapColumn("password"))
    assertEquals("name", ColumnUtils.wrapColumn("name"))
    XmllessInfoUtil.setDbType(XmllessDbType.POSTGRE_SQL)
    assertEquals("\"desc\"", ColumnUtils.wrapColumn("desc"))
    XmllessInfoUtil.setDbType(dbType)
  }

  @Test
  fun resolveColumnByPropertyName() {
    val result = ColumnsResolver.resolveColumnByPropertyName(
        "userName",
        createMethodInfo(StudentDAO::findByUserNameLike.javaMethod!!)
    )
    assertEquals(1, result.size)
    assertEquals("u.name AS name", result.first().toSql())
  }

}
